<template>
    <div @touchstart="touchStart" ref="panel" style="overflow: scroll;-webkit-overflow-scrolling : touch;" @scroll="infinite">
        <slot></slot>
        <div style="height: 45px;text-align: center; line-height: 45px" v-show="loading">
            <slot name="infinite-loading">
                <inline-loading></inline-loading>
                正在加载
            </slot>
        </div>
        <div v-show="!loading">
            <slot name="infinite-done">
                <divider slot="infinite-done">
                    <div style="line-height: 25px;">暂无更多内容</div>
                </divider>
            </slot>
        </div>
    </div>
</template>

<script>
    import {
        Divider,
        InlineLoading
    } from 'vux'

    export default {
        components: {
            Divider,
            InlineLoading
        },
        name: 'mu-scrollablePane',
        data: function () {
            return {
                loading: false,
                // Variables to track inputs
                startY: 0,

                startTopScroll: 0,
            }
        },
        computed: {
            totalPage: function () {
                return Math.ceil(this.total / this.pageSize)
            }
        },
        props: {
            total: {
                type: [Number],
                default: 1
            },
            pageSize: {
                type: [Number],
                default: 20
            },
            pageNum: {
                type: [Number],
                default: 1
            }
        },
        methods: {
            touchStart: function (e) {

                let elem = this.$refs.panel;

                if(elem.scrollTop <= 0)
                {
                    elem.scrollTop = 1;
                }

                if (elem.scrollHeight - elem.scrollTop <= elem.clientHeight)
                {
                    elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
                }
            },

            infinite: function (e) {

                if (e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight) {
                    if (this.pageNum < this.totalPage) {
                        //分页+1
                        let a = this.pageNum + 1
                        this.$emit('update:pageNum', a)

                        this.loading = true
                        this.$emit('on-infinite')
                    }
                    else {
                        this.loading = false
                    }
                }
            }
        }
    }

</script>
